package zero.tech.core.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zero.tech.core.data.KeyVal;

public class StringHelper {
	public static final String SPLID_PREFEX = "#####";

	public static String randUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replaceAll("-", "");
	}

	public static boolean isNullOrEmpty(String name) {
		if (name == null) {
			return true;
		}
		if ("".equals(name)) {
			return true;
		}
		return false;
	}

	public static boolean isNotEmpty(String str) {
		return !isNullOrEmpty(str);
	}

	public static String buildKey(Object... objs) {
		String str = null;
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(obj.toString()).append("-");
		}
		if (sb.length() > 0) {
			str = sb.substring(0, sb.length() - 1);
		}
		return str;
	}

	public static String build(Object... objs) {
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(String.valueOf(obj.toString()));
			sb.append(SPLID_PREFEX);
		}
		return sb.substring(0, sb.length() - SPLID_PREFEX.length());
	}

	public static String build(Collection<Object> objs) {
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(String.valueOf(obj.toString()));
			sb.append(SPLID_PREFEX);
		}
		return sb.substring(0, sb.length() - SPLID_PREFEX.length());
	}

	public static String getIP(String name) throws UnknownHostException {
		InetAddress address = null;
		try {
			address = InetAddress.getByName(name);
		} catch (UnknownHostException e) {
			throw e;
		}
		return address.getHostAddress().toString();
	}

	public static String sort(Object... objs) {
		List<String> list = new ArrayList<String>();
		for (Object obj : objs) {
			list.add(String.valueOf(obj.toString()));
		}
		Collections.sort(list);
		return build(list);
	}

	public static String randCode(int num) {
		String code = "";
		String tmp = "0123456789";
		String tmp1 = "ABCDEFGHJKLMNPQRSTUVWXYZ";
		int num1 = RandomUtil.randInt(1, num);
		int num2 = num - num1;
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < num1; i++) {
			if (i == 0) {
				int index = RandomUtil.randInt(tmp.length() - 1);
				String t = String.valueOf(tmp.charAt(index + 1));
				list.add(t);
			} else {
				int index = RandomUtil.randInt(tmp.length());
				String t = String.valueOf(tmp.charAt(index));
				list.add(t);
			}
		}

		for (int i = 0; i < num2; i++) {
			int index = RandomUtil.randInt(tmp1.length());
			String t = String.valueOf(tmp1.charAt(index));
			list.add(t);
		}

		// 字符串打乱顺序
		Collections.shuffle(list);
		for (String s : list) {
			code += s;
		}
		return code;
	}

	public static long randomRedPacketGold(long gold, int num) {
		// 如果当前剩余一个红包,则直接将所有剩余的钱给他
		long getGold;
		if (num == 1) {
			getGold = gold;
			return gold;
		} else {
			// 保证每个玩家应该至少能有0.01分钱(如果100块钱10个包 每个包平均10块钱) 平均值*2
			Random random = new Random();
			int minGold = 1;
			long maxGold = gold / num * 2;
			double money = random.nextDouble();
			money = money * maxGold;
			money = money <= minGold ? minGold : money;
			getGold = (int) money;
			// 需要 保证红包不能被抢成负数，而且 每个包必须有钱
			return getGold;
		}
	}

	public static Map<Integer, Integer> parseItemMap(String item_reward) {
		Map<Integer, Integer> map = new HashMap<>();
		String[] strs = item_reward.split(",");
		for (String str : strs) {
			Integer itemDefId = Integer.valueOf(str.split("#")[0]);
			Integer itemNum = Integer.valueOf(str.split("#")[1]);
			map.put(itemDefId, itemNum);
		}
		return map;
	}

	public static boolean containChinese(String str) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	public static <K, V> List<KeyVal<K, V>> parseMapToList(Map<K, V> map) {
		List<KeyVal<K, V>> list = new ArrayList<KeyVal<K, V>>();
		for (Entry<K, V> entry : map.entrySet()) {
			KeyVal<K, V> keyVal = new KeyVal<K, V>(entry.getKey(), entry.getValue());
			list.add(keyVal);
		}
		return list;
	}

	public static List<KeyVal<Integer, Integer>> parseStringToList(String str) {
		List<KeyVal<Integer, Integer>> list = new ArrayList<>();
		String[] strs = str.split(",");
		for (String tmp : strs) {
			String tmps[] = tmp.split("#");
			Integer scene_id = Integer.valueOf(tmps[0]);
			Integer times = Integer.valueOf(tmps[1]);
			KeyVal<Integer, Integer> kv = new KeyVal<Integer, Integer>(scene_id, times);
			list.add(kv);
		}
		return list;
	}

	public static void addToMap(Map<Integer, Integer> desmap, Map<Integer, Integer> srcmap) {
		for (Entry<Integer, Integer> entry : srcmap.entrySet()) {
			if (!desmap.containsKey(entry.getKey())) {
				desmap.put(entry.getKey(), entry.getValue());
			} else {
				Integer old = desmap.get(entry.getKey());
				desmap.put(entry.getKey(), old + entry.getValue());
			}
		}
	}

	public static String parseItemString(Map<Integer, Integer> itemmap) {
		String str="";
		for(Entry<Integer,Integer> entry:itemmap.entrySet()) {
			str+=entry.getKey()+"#"+entry.getValue()+",";
		}
		if(StringHelper.isNotEmpty(str)) {
			str=str.substring(0, str.length()-1);
		}
		return str;
	}
	
	public static void main(String[] args) {
		String str=sortBuildKey(1,5,4,2,6);
		System.out.println(str);
	}

	public static String sortBuildKey(Object...objs) {
		String result="";
		List<String> strs=new ArrayList<>();
		for(Object obj:objs) {
			strs.add(String.valueOf(obj));
		}
		Collections.sort(strs,new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		for(String str:strs) {
			result+=str+"#";
		}
		if(StringHelper.isNotEmpty(result)) {
			result=result.substring(0, result.length()-"#".length());
		}
		return result;
	}
}
